﻿using System;
using System.Collections.Generic;
using System.Text;
using SharpDiary.Core.ORMapping;
using System.Net.Mail;
using System.Net;
using System.Windows.Forms;

namespace SharpDiary.Diary
{
    /// <summary>
    /// 提供发送日记信息到指定 e-mail 的服务。
    /// </summary>
    public class MailSender: SmtpClient
    {
        #region Fields & Properties


        #endregion

        #region Constructors

        /// <summary>
        /// 初始化 MailSender 实例。
        /// </summary>
        /// <param name="userName">提供一个有效的用于通过 smtp 服务器发送电邮的用户名称。</param>
        /// <param name="password">提供一个有效的用于通过 smtp 服务器发送电邮的用户密码。</param>
        /// <param name="port">smtp 服务器端口</param>
        public MailSender(string smtpHost, int smtpPort, string smtpUserName, string smtpPassword, bool enableSSL)
        {
            this.Credentials = new NetworkCredential(smtpUserName, smtpPassword);
            this.Host = smtpHost;
            this.EnableSsl = enableSSL;
            this.Port = smtpPort;
        }

        #endregion

        #region Methods

        /// <summary>
        /// 发送日记信息到指定的 e-mail 地址。
        /// </summary>
        /// <param name="diaryInfo">日记信息</param>
        /// <param name="fromMail">源 e-mail</param>
        /// <param name="toMail">目标 e-mail</param>
        public void SendMailAsync(DiaryInfo diaryInfo, string fromMail, string toMail)
        {
            MailMessage mail = new MailMessage(fromMail, toMail);
            mail.SubjectEncoding = Encoding.UTF8;
            mail.BodyEncoding = mail.SubjectEncoding;
            mail.Subject = diaryInfo.Title;
            mail.Body = diaryInfo.Content;

            this.SendAsync(mail, null);
        }

        #endregion
    }
}
